package com.coldworks.coldjoke.ui;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.coldworks.base.manager.BaseNetworkManager;
import com.coldworks.coldjoke.R;
import com.coldworks.coldjoke.adapter.JokeQuickAdapter;
import com.coldworks.coldjoke.model.JokeEntity;
import com.coldworks.coldjoke.ui.JokeTabBar.OnTabChangeListener;
import com.coldworks.coldjoke.ui.MyListView.OnRefreshListener;
import com.coldworks.coldjoke.ui.MyListView.OnRefreshOlderListener;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class JokeQuickView extends RelativeLayout {
	protected final Logger log = LoggerFactory.getLogger(this.getClass());
	private Context context;
	JokeQuickViewDelegate delegate;
	private MyListView allListView;
	private MyListView hotListView;
	private MyListView shitListView;
	private JokeQuickAdapter jqcAdapter;
	private JokeQuickAdapter jqcHotAdapter;
	private JokeQuickAdapter jqcShitAdapter;
	private ImageButton showJokePageBtn;
	private ImageButton menuBtn;
	private JokeTabBar jokeTabBar;
	private JokeEntity jokeAll, jokeHot, jokeShit;
	private JokeEntity entityPlaying, entityDisplaying;

	public JokeQuickView(Context context) {
		super(context);
		setUpViews(context);
	}

	public JokeQuickView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setUpViews(context);
	}

	public JokeQuickView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		setUpViews(context);
	}

	public void start(JokeQuickViewDelegate pDelegate, JokeEntity jokeAll,
			JokeEntity jokeHot, JokeEntity jokeShit) {
		this.delegate = pDelegate;
		this.jokeAll = jokeAll;
		this.jokeHot = jokeHot;
		this.jokeShit = jokeShit;
		this.entityDisplaying = jokeAll;
		this.jqcAdapter = new JokeQuickAdapter(context, entityDisplaying);
		this.jqcHotAdapter = new JokeQuickAdapter(context, jokeHot);
		this.jqcShitAdapter = new JokeQuickAdapter(context, jokeShit);
		this.allListView.setAdapter(jqcAdapter);
		this.hotListView.setAdapter(jqcHotAdapter);
		this.shitListView.setAdapter(jqcShitAdapter);
		this.setDisplaySort(jokeAll, allListView);
		this.showLoadingTip();
	}

	private void setUpViews(final Context context) {
		this.context = context;
		RelativeLayout view = (RelativeLayout) LayoutInflater.from(context)
				.inflate(R.layout.navigation_joke_quick_view, this, false);
		allListView = (MyListView) view.findViewById(R.id.all_listview);
		hotListView = (MyListView) view.findViewById(R.id.hot_listview);
		shitListView = (MyListView) view.findViewById(R.id.shit_listview);
		menuBtn = (ImageButton) view.findViewById(R.id.button_menu);
		showJokePageBtn = (ImageButton) view
				.findViewById(R.id.button_show_joke);
		jokeTabBar = (JokeTabBar) view.findViewById(R.id.joke_tab);

		allListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View cell,
					int position, long arg3) {
				entityPlaying = entityDisplaying;
				entityPlaying.location = position - 1;
				delegate.JQVselect(entityPlaying);
			}
		});

		allListView.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				delegate.JQVgetNew();

			}
		});

		allListView.setOnRefreshOlderListener(new OnRefreshOlderListener() {

			@Override
			public void onRefresh() {
				delegate.JQVgetNext();

			}
		});

		hotListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View cell,
					int position, long arg3) {
				entityPlaying = entityDisplaying;
				entityPlaying.location = position - 1;
				delegate.JQVselect(entityPlaying);
			}
		});

		hotListView.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				delegate.JQVgetNew();

			}
		});

		hotListView.setOnRefreshOlderListener(new OnRefreshOlderListener() {

			@Override
			public void onRefresh() {
				delegate.JQVgetNext();

			}
		});

		shitListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View cell,
					int position, long arg3) {
				entityPlaying = entityDisplaying;
				entityPlaying.location = position - 1;
				delegate.JQVselect(entityPlaying);
			}
		});

		shitListView.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				delegate.JQVgetNew();

			}
		});

		shitListView.setOnRefreshOlderListener(new OnRefreshOlderListener() {

			@Override
			public void onRefresh() {
				delegate.JQVgetNext();

			}
		});

		menuBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				delegate.JQVshowMenu();
			}
		});

		jokeTabBar.setOnTabChangeListener(new OnTabChangeListener() {
			@Override
			public void onTabChanged(int tab) {

				switch (tab) {
				case 0:
					setDisplaySort(jokeAll, allListView);
					allListView.setVisibility(View.VISIBLE);
					hotListView.setVisibility(View.GONE);
					shitListView.setVisibility(View.GONE);
					break;
				case 1:
					setDisplaySort(jokeHot, hotListView);
					hotListView.setVisibility(View.VISIBLE);
					allListView.setVisibility(View.GONE);
					shitListView.setVisibility(View.GONE);
					break;
				case 2:
					setDisplaySort(jokeShit, shitListView);
					shitListView.setVisibility(View.VISIBLE);
					hotListView.setVisibility(View.GONE);
					allListView.setVisibility(View.GONE);
					break;
				default:
					break;
				}
				// jqcAdapter.notifyDataSetChanged();
			}

			@Override
			public void onTabClickedAgain(int tab) {
				switch (tab) {
				case 0:
					allListView.setSelection(1);
					break;
				case 1:
					hotListView.setSelection(1);
					break;
				case 2:
					shitListView.setSelection(1);
					break;
				default:
					break;
				}
				return;
			}

		});

		showJokePageBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				delegate.JQVshowView();
			}
		});
		addView(view);
	}

	public void setDisplaySort(JokeEntity jokeEntity, MyListView listView) {
		this.entityDisplaying = jokeEntity;
		int location = jokeEntity.location;
		jqcAdapter.setJokeEntity(jokeEntity);
		listView.setSelection(location);
		jqcAdapter.notifyDataSetChanged();
		showLoadingTip();
		if (!jokeEntity.manager.getStart()) {
			hideLoadingTip();
		}
	}

	public void reload(JokeEntity pEntityPlaying) {
		log.info("JQV reload type:" + pEntityPlaying.manager.getJokesSort()
				+ " size:" + pEntityPlaying.manager.getListShow().size()
				+ " location:" + pEntityPlaying.location);
		if (entityDisplaying != pEntityPlaying) {

			if (pEntityPlaying == jokeAll) {
				jokeTabBar.setTabSelected(0);
				setDisplaySort(pEntityPlaying, allListView);
			} else if (pEntityPlaying == jokeHot) {
				jokeTabBar.setTabSelected(1);
				setDisplaySort(pEntityPlaying, hotListView);
			} else {
				jokeTabBar.setTabSelected(2);
				setDisplaySort(pEntityPlaying, shitListView);
			}
		}
		allListView.setSelection(pEntityPlaying.location);
		return;
	}

	// 1. 如果当前显示的类型与pJokeManager的类型不符，什么都不做，返回
	// 2. reloadData并偏移到原来的那一则
	// 3. 如果newCount==0,则停止loading态
	public void doneNew(JokeEntity jokeEntity, int newCount) {
		log.info("JQV doneNew type:" + jokeEntity.manager.getJokesSort() + " "
				+ newCount);
		if (jokeEntity != entityDisplaying) {
			return;
		}
		if (jokeEntity == jokeAll) {
			jqcAdapter.setJokeEntity(jokeEntity);
			allListView.onRefreshComplete();
			jqcAdapter.notifyDataSetChanged();
		} else if (jokeEntity == jokeHot) {
			jqcHotAdapter.setJokeEntity(jokeEntity);
			hotListView.onRefreshComplete();
			jqcHotAdapter.notifyDataSetChanged();
		} else {
			jqcShitAdapter.setJokeEntity(jokeEntity);
			shitListView.onRefreshComplete();
			jqcShitAdapter.notifyDataSetChanged();
		}

		// if (newCount == 0) {
		// this.hideLoadingTip();
		// } else {
		// // this.showAutoHideTip("发现最新" + newCount + "条冷货", newCount, 0);
		// allListView.setSelection(1);
		// }
	}

	// 1. 如果当前显示的类型与pJokeManager的类型不符，什么都不做，返回
	// 2. reloadData并保持显示位置不变
	// 3. 如果nextCount==0,则停止loading态
	public void doneNext(JokeEntity jokeEntity, int nextCount) {
		log.info("JQV doneNext type:" + jokeEntity.manager.getJokesSort() + " "
				+ nextCount);
		if (jokeEntity != entityDisplaying) {
			return;
		}
		if (jokeEntity == jokeAll) {
			jqcAdapter.setJokeEntity(jokeEntity);
			allListView.onRefreshOlderComplete();
			jqcAdapter.notifyDataSetChanged();
		} else if (jokeEntity == jokeHot) {
			jqcHotAdapter.setJokeEntity(jokeEntity);
			hotListView.onRefreshOlderComplete();
			jqcHotAdapter.notifyDataSetChanged();
		} else {
			jqcShitAdapter.setJokeEntity(jokeEntity);
			shitListView.onRefreshOlderComplete();
			jqcShitAdapter.notifyDataSetChanged();
		}

		// allListView.onRefreshOlderComplete();

		// if (nextCount == 0) {
		// // this.showAutoHideTip("正在加载，请稍后再试", 0, 0);
		// } else {
		// jqcAdapter.setJokeEntity(jokeEntity);
		// jqcAdapter.notifyDataSetChanged();
		// // this.showAutoHideTip("发现之前" + nextCount + "条冷货", 0, nextCount);
		// }
	}

	public void doneNewAndNext(JokeEntity jokeEntity, int newCount,
			int nextCount) {
		log.info("JQV doneNewAndNext type:" + jokeEntity.manager.getJokesSort()
				+ " " + newCount + "+" + nextCount);
		if (jokeEntity != entityDisplaying) {
			return;
		}
		jqcAdapter.setJokeEntity(jokeEntity);
		jqcAdapter.notifyDataSetChanged();

		if (newCount + nextCount == 0) {
			// this.showAutoHideTip("正在加载，请稍后再试", 0, 0);
			return;
		}
		if (newCount > 0) {
			allListView.setSelection(1);
		}
		// this.showAutoHideTip("发现" + nextCount + "条冷货", newCount, nextCount);
	}

	public void refreshNew() {
		if (!BaseNetworkManager.getInstance().isNetworkAvailable(context)) {
			Toast.makeText(context, "服务器出小差了！稍后再试~", Toast.LENGTH_SHORT).show();
			return;
		}
		if (entityDisplaying.manager.isGettingNew()) {
			log.info("already getting new");
			return;
		}
		this.showLoadingTip();
		this.delegate.JQVgetNew();
	}

	public void refreshNext() {
		if (!BaseNetworkManager.getInstance().isNetworkAvailable(context)) {
			Toast.makeText(context, "服务器出小差了！稍后再试~", Toast.LENGTH_SHORT).show();
			return;
		}
		this.showLoadingTip();
		this.delegate.JQVgetNext();
	}

	public void refreshNewAndNext() {
		if (!BaseNetworkManager.getInstance().isNetworkAvailable(context)) {
			Toast.makeText(context, "服务器出小差了！稍后再试~", Toast.LENGTH_SHORT).show();
			return;
		}
		if (entityDisplaying.manager.isGettingNew()) {
			log.info("already getting new or next");
			return;
		}
		this.showLoadingTip();
		this.delegate.JQVgetNewAndNext();
	}

	public void viewLeftOrRight() {
		boolean leftNow = true;
		if (leftNow) {
			this.viewRight();
		} else {
			this.viewLeft();
		}
	}

	public void viewLeft() {
		allListView.setSelection(1);
	}

	public void viewRight() {
		allListView.setSelection(entityPlaying.manager.getListShow().size());
	}

	public void zoomIn() {
		final ScaleAnimation scaleAnimation = new ScaleAnimation(2, 1f, 2, 1f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		scaleAnimation.setDuration(300);
		scaleAnimation.setStartOffset(200);
		scaleAnimation.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {

			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				JokeQuickView.this.setVisibility(View.VISIBLE);
			}
		});
		final AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
		alphaAnimation.setDuration(400);
		alphaAnimation.setStartOffset(100);
		final TranslateAnimation translateAnimation = new TranslateAnimation(0,
				0, 300, 0);
		translateAnimation.setDuration(400);
		translateAnimation.setStartOffset(100);

		AnimationSet set = new AnimationSet(true);
		set.addAnimation(scaleAnimation);
		set.addAnimation(alphaAnimation);
		set.addAnimation(translateAnimation);
		startAnimation(set);
	}

	public void zoomOut() {
		final ScaleAnimation scaleAnimation = new ScaleAnimation(1, 2f, 1, 2f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		scaleAnimation.setDuration(600);
		final AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
		alphaAnimation.setDuration(600);
		final TranslateAnimation translateAnimation = new TranslateAnimation(0,
				0, 0, 300);
		translateAnimation.setDuration(600);
		AnimationSet set = new AnimationSet(true);

		scaleAnimation.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {

			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				JokeQuickView.this.setVisibility(View.GONE);
			}
		});
		set.addAnimation(scaleAnimation);
		set.addAnimation(alphaAnimation);
		set.addAnimation(translateAnimation);
		startAnimation(set);
	}

	public void showLoadingTip() {
		// menuBtn.setVisibility(View.GONE);
		// tipText.setVisibility(View.VISIBLE);
		// tipText.setText("正在制冷...");
	}

	public void hideLoadingTip() {
		if (entityDisplaying.manager.isGettingNew()) {
			return;
		}
		// tipText.setVisibility(View.INVISIBLE);
		// menuBtn.setVisibility(View.VISIBLE);
		// indicator.alpha = 0;
	}

	// public void showAutoHideTip(String tipStr, int newCount, int nextCount) {
	// this.hideLoadingTip();
	// tipText.setText(tipStr);
	// AlphaAnimation010 alphaText = new AlphaAnimation010(tipText);
	// tipText.setAnimation(alphaText);
	// alphaText.start();
	//
	// if (newCount > 0) {
	// AlphaAnimation010 alphaLeft = new AlphaAnimation010(tipText);
	// leftBtn.setAnimation(alphaLeft);
	// alphaLeft.start();
	// }
	// if (nextCount > 0) {
	// AlphaAnimation010 alphaRight = new AlphaAnimation010(rightBtn);
	// rightBtn.setAnimation(alphaRight);
	// alphaRight.start();
	// }
	//
	// }

	class AlphaAnimation010 extends AlphaAnimation {
		public AlphaAnimation010(Context context, AttributeSet attrs) {
			super(context, attrs);
		}

		public AlphaAnimation010(final View animView) {
			super(0, 1);
			this.setDuration(500);
			this.setAnimationListener(new AnimationListener() {
				@Override
				public void onAnimationStart(Animation animation) {
					animView.setVisibility(VISIBLE);
				}

				@Override
				public void onAnimationRepeat(Animation animation) {
				}

				@Override
				public void onAnimationEnd(Animation animation) {
					final AlphaAnimation alpha10 = new AlphaAnimation(1, 0);
					alpha10.setDuration(500);
					alpha10.setStartOffset(500);
					alpha10.setAnimationListener(new AnimationListener() {
						@Override
						public void onAnimationStart(Animation animation) {
						}

						@Override
						public void onAnimationRepeat(Animation animation) {
						}

						@Override
						public void onAnimationEnd(Animation animation) {
							animView.setVisibility(INVISIBLE);
						}
					});
					animView.setAnimation(alpha10);
					alpha10.start();
				}
			});
		}

	}

	public interface JokeQuickViewDelegate {
		/**
		 * 通知Fragment切换为正常模式
		 */
		void JQVshowView();

		/**
		 * 通知Fragment选择的笑话种类和位置
		 */
		void JQVselect(JokeEntity jokeEntity);

		/**
		 * 通知Fragment用户需要刷新更早的
		 */
		void JQVgetNew();

		/**
		 * 通知Fragment用户需要刷新更老的
		 */
		void JQVgetNext();

		/**
		 * 通知Fragment用户需要刷新更
		 */
		void JQVgetNewAndNext();

		/**
		 * 通知Fragment展示菜单栏
		 */
		void JQVshowMenu();
	}
}
